package com.inspur.gs.sbssdl.dzyhpgl.dzyhphz.dzyhphz.back.bizactions;
import com.inspur.edp.bef.api.lcp.IStandardLcp;
import com.inspur.edp.bef.core.LcpUtil;
import com.inspur.edp.bef.api.be.*;
import com.inspur.edp.bef.api.action.VoidActionResult;
import com.inspur.edp.bef.engine.entity.AssociationInfo;
import com.inspur.edp.bef.spi.action.AbstractManagerAction;
import com.inspur.edp.cef.entity.changeset.ModifyChangeDetail;
import com.inspur.edp.cef.entity.entity.IEntityData;
import com.inspur.edp.cef.entity.entity.IEntityDataCollection;
import com.inspur.edp.cef.entity.entity.IValuesContainer;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class sbssYhpHzdupdKcslMgrAction extends AbstractManagerAction<VoidActionResult> {
	private String id;
	public sbssYhpHzdupdKcslMgrAction(IBEManagerContext managerContext,
			String id) {
		super(managerContext);
		this.id = id;
	}
	private String hzdCfg = "com.inspur.gs.sbssdl.dzyhpgl.dzyhphz.dzyhphz.back.sbssYhpHzd";
	private String kcCfg = "com.inspur.gs.sbssdl.dzyhpgl.dzyhyu.dzyhyu.back.sbssYhpItem";

	/**
	 * 提交库存转
	 */
	@Override
	public  void execute() {
		IStandardLcp lcp = LcpUtil.getLcp(hzdCfg);
		IStandardLcp kcLcp = LcpUtil.getLcp(kcCfg);
		// hzd
		IEntityData hzdData = lcp.retrieve(this.id).getData();
		IEntityDataCollection hzdMx = hzdData.getChilds().get("hzdMx");

		for (IEntityData mx : hzdMx) {
			submitKc(hzdData,mx,kcLcp);
		}
	}
	private IBusinessEntity getEntity(String dataId) {
		return (IBusinessEntity)super.getBEManagerContext().getEntity(dataId);
	}	private IBEService getMgr() {
		return (IBEService)super.getBEManagerContext().getBEManager();
	}

	// 提交库存更改
	private void submitKc(IEntityData hzdData, IEntityData mx, IStandardLcp kcLcp) {
		String mxId = mx.getID();
		int lockNum = Integer.parseInt(mx.getValue("zcsl").toString());
		IValuesContainer kcdh = (IValuesContainer) mx.getValue("kcdh");
		if (kcdh == null || kcdh.getValue("kcdh") == null) {
			return;
		}
		String kcId = kcdh.getValue("kcdh").toString();
		IEntityData kcData = kcLcp.retrieve(kcId).getData();
		int tsl = Integer.parseInt(kcData.getValue("tsl").toString());

		// 检查日志是否已经提交,已经提交则不进行修改
		IEntityDataCollection itemtras = kcData.getChilds().get("itemtras");
		boolean hasItem = itemtras.stream().anyMatch(item -> item.getValue("djMxid") != null && item.getValue("djMxid").toString().equals(mxId) && item.getValue("trasStatus").toString().equals("1"));
		if (hasItem) return;

		// 修改数量
		ModifyChangeDetail kcMcd = new ModifyChangeDetail(kcId);
		kcMcd.putItem("tsl", tsl - lockNum);

		// 修改总价
		BigDecimal sl = new BigDecimal(tsl);
		BigDecimal dj =  (BigDecimal) kcData.getValue("wlDj");
		kcMcd.putItem("zj",sl.multiply(dj));

		itemtras.stream().filter(i-> i.getValue("djMxid") != null && i.getValue("djMxid").toString().equals(mxId))
				.forEach(i->{
					ModifyChangeDetail kcMxMcd = new ModifyChangeDetail(i.getID());
					kcMxMcd.getPropertyChanges().put("trasStatus","1");
					kcMcd.addChildChangeSet("itemtras",kcMxMcd);
				});
		// 修改转出为审核完毕
		kcLcp.modify(kcMcd);

		// 新增库存单
		IEntityData kcDataNew = kcLcp.retrieveDefault();
		ModifyChangeDetail newMcd = copyIEntityData(kcData, kcDataNew);

		// 修改数量
		newMcd.putItem("tsl", lockNum);
		newMcd.putItem("sysl", lockNum);
		BigDecimal lockDec = new BigDecimal(lockNum);
		newMcd.putItem("zj", lockDec.multiply(dj));
		newMcd.putItem("fromType","2");

		// 使用人
		IValuesContainer zrr = (IValuesContainer) mx.getValue("zrrId");
		String zrrId = zrr.getValue("zrrId").toString();
		AssociationInfo zrrIdInfo = new AssociationInfo();
		zrrIdInfo.setValue("syryid", zrrId);
		newMcd.putItem("syryid", zrrIdInfo);

		// 归口部门
		IValuesContainer rgkbm = (IValuesContainer) hzdData.getValue("rgkbm");
		String rgkbmId = rgkbm.getValue("rgkbm").toString();
		AssociationInfo rgkbmInfo = new AssociationInfo();
		rgkbmInfo.setValue("gkbmid", rgkbmId);
		newMcd.putItem("gkbmid", rgkbmInfo);

		// 生成流转记录
		ArrayList<String> nodeCodes = new ArrayList<>();
		nodeCodes.add("itemtras");
		ArrayList<String> hierarchyIds = new ArrayList<>();
		hierarchyIds.add(kcDataNew.getID());
		IEntityData kcChild = kcLcp.retrieveDefaultChild(nodeCodes, hierarchyIds);

		ModifyChangeDetail mxDetail = new ModifyChangeDetail(kcChild.getID());
		mxDetail.getPropertyChanges().put("trasType","1");
		mxDetail.getPropertyChanges().put("trasStatus","1");
		mxDetail.getPropertyChanges().put("omission","0");
		mxDetail.getPropertyChanges().put("trasSl",lockNum);
		mxDetail.getPropertyChanges().put("djid",hzdData.getID());//流转单据
		mxDetail.getPropertyChanges().put("djMxid",mx.getID());
		mxDetail.getPropertyChanges().put("djCode",hzdData.getValue("djbh"));

		newMcd.addChildChangeSet("itemtras",mxDetail);

		kcLcp.modify(newMcd);

		kcLcp.save();
	}



	private ModifyChangeDetail copyIEntityData (IEntityData data, IEntityData copyData){
		ModifyChangeDetail detail = new ModifyChangeDetail(copyData.getID());
		List<String> dataPropertyNames = data.getPropertyNames();
		for (String s : dataPropertyNames) {
			if (!s.equals("ID")){
				if (data.getValue(s) != null){
					if (data.getValue(s) instanceof IValuesContainer){
						AssociationInfo info = new AssociationInfo();
						Object value;
						try {
							value = ((IValuesContainer) data.getValue(s)).getValue(s);
						}catch (RuntimeException e){
							value = null;
						}
						if (value != null){
							info.setValue(s, value);
							detail.getPropertyChanges().put(s, info);
						}
					}else {
						detail.putItem(s,data.getValue(s));
					}
				}
			}
		}
		return detail;
	}
}